import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '../store'
import layout from '@/layout'



Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login', hidden:true },
  { path: '/login', 
    component: () => import('../components/Login.vue'),
    hidden: true
  },
  {
    path: '/home',
    name: 'Home',
    component: layout
  },
  {
    path: '/about',
    name: 'About',
    component: layout,
    children:[
      {
        path: '/message',
        name: 'Message',
        component: () => import('@/views/Message.vue')
      }
    ]
  },
  {
    path: '/info',
    name: 'Info',
    component: layout,
    children:[
      {
        path: '/news',
        name: 'News',
        component: () => import('@/views/News.vue')
      }
    ]
  },
  { path: '/404', hidden:true, component: () => import('@/components/404.vue')},
  { path: '/401', hidden:true, component: () => import('@/components/401.vue')},
  { path: '*', redirect: '/404'}
]
const asyncRoutes  = [
  {
    path: '/admin',
    name: 'Admin',
    component: layout,
    meta:{
      roles:['admin']
    },
    children:[
      {
        path: '/white',
        name: 'white',
        component: () => import('@/views/White.vue')
      }
    ]
  },
  {
    path: '/user',
    name: 'User',
    meta:{
      roles:['admin', 'user']
    },
    component: layout,
    children:[
      {
        path: '/news',
        name: 'News',
        component: () => import('@/views/User.vue')
      }
    ]
  },
  {
    path: '/editor',
    name: 'Editor',
    meta:{
      roles:['admin', 'user', 'editor']
    },
    component: layout,
    children:[
      {
        path: '/boss',
        name: 'Boss',
        meta:{
          roles:['admin', 'boss']
        },
        component: () => import('@/views/Boss.vue')
      }
    ]
  },
  {
    path: '/white',
    name: 'White',
    meta:{
      roles:['admin', 'user', 'boss', 'editor']
    },
    component: layout,
    children:[
      {
        path: '/test',
        name: 'Test',
        meta:{
          roles:['admin', 'boss', 'editor']
        },
        component: () => import('@/views/Test.vue')
      },
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
  asyncRoutes 
})
// router.beforeEach((to, from, next) => {
//   if(to.path === '/home'){
//       store.dispatch('getRole').then((res)=> {
//         const { role } = res[1]
//         const async = router.options.asyncRoutes.filter(item => item.meta.roles.includes(role))
//         const publicRouter = router.options.routes.filter(item => item.children!= null)
//         router.addRoutes(publicRouter)
//         router.addRoutes(async)
//       })
//     // router.addRoutes(store.state.menuList)
//   }
//   next()
// })
// router.beforeEach((to,from,next)=>{
//     // store.dispatch('getInfo')
//     if(to.path==='/404') {
//       if(store.state.role=='user') {
//         next({path:404})
//       }

//         next(`${to.path}`)
//     }
//   next()
// })
export default router
